package com.okyai.cms.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.okyai.common.config.Global;
import com.okyai.common.config.ServerConfig;
import com.okyai.common.core.controller.BaseController;
import com.okyai.common.core.domain.AjaxResult;
import com.okyai.common.utils.file.FileUploadUtils;

/**
 * cms模块的通用操作
 */
@Controller
public class CmsController extends BaseController {

    @Autowired
    private ServerConfig serverConfig;


    /**
     * 上传素材
     */
    @PostMapping("/cms/uploadMaterial")
    @ResponseBody
    public AjaxResult uploadMaterial(MultipartFile file) throws Exception
    {
    	Map<String,Object> data=new HashMap<String,Object>();
        try
        {
            // 上传并返回新文件名称
        	int width=FileUploadUtils.getImgWidth(file);
            int height=FileUploadUtils.getImgHeight(file);
            
            data.put("suffix",FileUploadUtils.getExtension(file));
            data.put("size",file.getSize());
            data.put("name",file.getOriginalFilename());
            
            String path = FileUploadUtils.upload(Global.getMaterialPath(), file);
            String url = serverConfig.getUrl() + path;
            data.put("path",path);
            data.put("url",url);
            data.put("width",width);
            data.put("height",height);
            return AjaxResult.success(data);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

  
}
